import { defineStore } from 'pinia'
import { ref } from 'vue'

export default defineStore('requestHeaderFromQueryParam', () => {
  const requestHeaderFromQueryParam = ref<Record<string, string>>({})

  const setRequestHeaderFromQueryParam = () => {
    let href = window.location.href

    const index = href.indexOf('?')
    if (index < 0) {
      return null
    }

    href = href.substring(index + 1)
    if (!href) {
      return null
    }

    href.split('&').forEach((item) => {
      const [key, value] = item.split('=')
      requestHeaderFromQueryParam.value[key] = value
    })
    console.log(requestHeaderFromQueryParam.value)
  }

  return {
    requestHeaderFromQueryParam,
    setRequestHeaderFromQueryParam,
  }
})
